package com.yiduo.outpost.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yiduo.outpost.core.model.enums.UserRole;
import com.yiduo.outpost.core.redis.props.RedisConstant;
import com.yiduo.outpost.core.security.UnAuthorizedException;
import com.yiduo.outpost.core.model.exception.Asserts;
import com.yiduo.outpost.core.model.exception.ErrorConstants;
import com.yiduo.outpost.ucenter.mapper.UserMapper;
import com.yiduo.outpost.ucenter.service.UserService;
import com.yiduo.outpost.core.model.entity.User;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Optional;


/**
* @author yanwen
* @description 针对表【users】的数据库操作Service实现
* @createDate 2023-07-25 10:59:46
*/
@Service
@CacheConfig(cacheNames = RedisConstant.USER_SESSION_CACHE_KEY)
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService , UserDetailsService {

    @Cacheable(key = "#userId")
    public User getUserWithPermissions(Long userId) {
        User user = getById(userId);
        Asserts.assertNotNull(user, UnAuthorizedException::new);
//        List<Permission> permissions = new ArrayList<>();
//        if (user.getRole() == UserRole.ROLE_ADMIN) {
//            permissions.addAll(permissionService.selectAll());
//        } else if (user.getPermissionGroupId() != null) {
//            permissions.addAll(permissionService.findByGroupId(user.getPermissionGroupId()));
//        }
//        permissions.addAll(permissionConfig.permissions);
//        user.setPermissions(permissions);
        return user;
    }

    /**
     * 清理用户缓存
     */
    @CacheEvict(key = "#userId")
    public void clearUserCache(Long userId) {

    }

    /**
     * 根据用户名获取用户信息 security 用户信息验证需要
     * @param username the username identifying the user whose data is required.
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user  = this.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username));
        return Optional.ofNullable(user).orElseThrow(() -> new UsernameNotFoundException(ErrorConstants.ERROR_USER_NOT_EXIST));
    }

    public static void main(String[] args) {
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode("111111");
        System.out.println(encode);
    }
}
